
package com.jojoteamwork.photo.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.http.AndroidHttpClient;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class HttpUtils {

    public static final String DOMAIN = "http://wad2.net/otaku_photo/update_info/";

    public static final String SITE_MAGAZINE = DOMAIN + "magazine/";

    public static final String SITE_COVER = DOMAIN + "cover/";

    public static final String SITE_UPGRADE = "http://wad2.net/otaku_photo/upgrade.json";

    private static final int TIMEOUT = 10000;

    public static final int STATE_CONNECT_NONE = 0;

    public static final int STATE_CONNECT_WIFI = 1;

    public static final int STATE_CONNECT_MOBILE = 2;

    public static String loadString(String url) {
        AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
        final HttpGet getRequest = new HttpGet(url);
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, TIMEOUT);
        getRequest.setParams(params);
        StringBuilder builder = new StringBuilder();
        try {
            HttpResponse response = client.execute(getRequest);
            final int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                return null;
            }

            final HttpEntity entity = response.getEntity();
            if (entity != null) {
                BufferedReader reader = null;
                try {
                    reader = new BufferedReader(new InputStreamReader(entity.getContent(), "utf-8"));

                    String buff;
                    while ((buff = reader.readLine()) != null) {
                        builder.append(buff);
                    }

                } finally {
                    if (reader != null) {
                        reader.close();
                    }
                    entity.consumeContent();
                }
            }
        } catch (Exception e) {
            getRequest.abort();
        } finally {
            if ((client instanceof AndroidHttpClient)) {
                ((AndroidHttpClient) client).close();
            }
        }
        return builder.toString();
    }

    /**
     * Simple network connection check.
     * 
     * @param context
     */
    public static boolean checkConnection(Context context) {
        final ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo == null || !networkInfo.isConnectedOrConnecting()) {
            return false;
        }
        return true;
    }

    public static int getNetConnectState(Context context) {
        final ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if (networkInfo != null && networkInfo.isConnected()) {
            return STATE_CONNECT_WIFI;
        }
        networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (networkInfo != null && networkInfo.isConnected()) {
            return STATE_CONNECT_MOBILE;
        }
        return STATE_CONNECT_NONE;
    }

}
